package io.github.wanggit.antrpc.commons.bean;

import org.apache.curator.utils.ZKPaths;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class ConfigItem extends ConfigItemData implements Serializable {

    private static final long serialVersionUID = 1988102096553237747L;

    /** 在ZooKeeper中的全路径 */
    private String fullPath;

    /** 优先级 */
    private int priority;

    @Override
    public String toString() {
        return "key="
                + getKey()
                + ", value="
                + getValue()
                + ", fullPath="
                + fullPath
                + ", priority="
                + priority
                + ", ts="
                + (null == getTs()
                        ? ""
                        : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(getTs())));
    }

    @Override
    public int hashCode() {
        return getKey().hashCode() + fullPath.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof ConfigItem)) {
            return false;
        }
        ConfigItem other = (ConfigItem) obj;
        return Objects.equals(getKey(), other.getKey())
                && Objects.equals(fullPath, other.getFullPath());
    }

    public String getFullPath() {
        return fullPath;
    }

    /**
     * 从
     * /__rpc_cnf__/default/InterfaceBreakerConfigsChangeWorkerI/antrpc.interface-breaker-configs.io.github.wanggit.antrpc.commons.bean.ConfigItemData.enabled
     * 中获取得
     * antrpc.interface-breaker-configs.io.github.wanggit.antrpc.commons.bean.ConfigItemData.enabled
     *
     * @return fullkey
     */
    @Deprecated
    public String getFullKey() {
        List<String> tmps = ZKPaths.split(getFullPath());
        return tmps.get(tmps.size() - 1);
    }

    public void setFullPath(String fullPath) {
        this.fullPath = fullPath;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }
}
